package cn.laniakea.solrj;

import cn.laniakea.solrj.pojo.Item;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;

import java.util.List;


public class SolrjQuery {
    /**
     * document方式查询
     * @throws SolrServerException
     */
    @Test 
    public void testQuery() throws SolrServerException {
        //创建sorl服务对象
        HttpSolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr/core1");
        //查询对象 
        SolrParams solrParams=new SolrQuery("*:*");
        //执行查询,获取响应
        QueryResponse response = solrServer.query(solrParams);
        SolrDocumentList documentList = response.getResults();
        System.out.println("一共有文档数:"+documentList.getNumFound());
        for (SolrDocument solrDocument : documentList) {
            System.out.print("id:"+solrDocument.get("id"));
            System.out.print(",title:"+solrDocument.get("title"));
            System.out.print(",price:"+solrDocument.get("price"));
            System.out.println();

        }

    }

    /**
     * bean方式查询
     * @throws SolrServerException
     */
    @Test
    public void testQueryBean() throws SolrServerException {
        //创建sorl服务对象
        HttpSolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr/core1");
        //查询对象
        SolrParams solrParams=new SolrQuery("*:*");
        //执行查询,获取响应
        QueryResponse response = solrServer.query(solrParams);
        List<Item> itemList = response.getBeans(Item.class);
        System.out.println("一共有文档数:"+response.getResults().getNumFound());
        for (Item item : itemList) {
            System.out.println(item);
        }

    }

    /**
     * 特殊方式查询
     * @throws SolrServerException
     */
    @Test
    public void testQueryBoolean() throws SolrServerException {
        //创建sorl服务对象
        HttpSolrServer solrServer=new HttpSolrServer("http://localhost:8080/solr/core1");
        //查询对象
        SolrParams solrParams=new SolrQuery("(id:2 OR id:3) AND (id:3 OR id:4)");
        //执行查询,获取响应
        QueryResponse response = solrServer.query(solrParams);
        List<Item> itemList = response.getBeans(Item.class);
        System.out.println("一共有文档数:"+response.getResults().getNumFound());
        for (Item item : itemList) {
            System.out.println(item);
        }

    }

    @Test
    public void testQueryFuzzay() throws SolrServerException {
        //创建sorl服务对象
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        //查询对象
        SolrParams solrParams = new SolrQuery("title:dipae~2");
        //执行查询,获取响应
        QueryResponse response = solrServer.query(solrParams);
        List<Item> itemList = response.getBeans(Item.class);
        System.out.println("一共有文档数:" + response.getResults().getNumFound());
        for (Item item : itemList) {
            System.out.println(item);
        }


    }

    @Test
    public void testQueryNumberRange() throws SolrServerException {
        //创建sorl服务对象
        HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core1");
        //查询对象
        //  SolrParams solrParams = new SolrQuery("price:[299900 TO 399900]");
        SolrParams solrParams = new SolrQuery("id:[1 TO 2]");//用字符的开头去匹配
        //执行查询,获取响应
        QueryResponse response = solrServer.query(solrParams);
        List<Item> itemList = response.getBeans(Item.class);
        System.out.println("一共有文档数:" + response.getResults().getNumFound());
        for (Item item : itemList) {
            System.out.println(item);
        }


    }
}
